package com.cfpamf.ms.insur.pay.job;

import com.cfpamf.ms.insur.pay.dao.PayRefundMapper;
import com.cfpamf.ms.insur.pay.pojo.po.PayRefundPO;
import com.cfpamf.ms.insur.pay.service.InsurPayService;
import com.cfpamf.ms.insur.pay.util.XxlLogger;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 退款状态补偿
 *
 * @author zhangnayi
 */
@Slf4j
@Component
@JobHandler(value = "refund-make-up")
public class RefundMakeUpJobHandler extends IJobHandler {

    @Autowired
    private InsurPayService insurPayService;

    @Autowired
    private PayRefundMapper refundMapper;

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        XxlLogger.info(log, "=============退款状态补偿 start==============");
        List<PayRefundPO> payRefunds = refundMapper.selectRefundingPayRefundList();
        payRefunds.forEach(entity -> {
            try {
                XxlLogger.info(log, String.format("退款状态补偿 refundTrxNo=%s", entity.getRefundTrxNo()));
                insurPayService.syncPayRefundInfoWithBank(entity.getSourceSys(), entity.getOrderNo(), entity.getPayType());
            } catch (Exception e) {
                XxlLogger.error(log, e);
            }
        });
        XxlLogger.info(log, "=============退款状态补偿 start==============");
        return ReturnT.SUCCESS;
    }
}
